package com.qiyi.manager;
 
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.qiyi.bean.Course;
import com.qiyi.database.DBHelper;

/**
 * @param  
 * @return
 */
public class CourseManager {  
      
        private DBHelper helper;  
        
        
        public CourseManager(Context context) {  
            helper = new DBHelper(context);//初始化数据库  
            
        }  
      
        /**  
         * 插入一个Course  
         * @param p 要插入的Course  
         */  
        public void insert(Course c) {  
            SQLiteDatabase db = helper.getWritableDatabase();  //获取到数据库  
            db.execSQL("INSERT INTO Course(name) VALUES(?,?,?)", new Object[] { c.getName() });  
            db.close();  
        }  
      
        /**  
         * 根据ID删除  
         * @param id 要删除的Course的ID  
         */  
        public void delete(Integer id) {  
            SQLiteDatabase db = helper.getWritableDatabase();  
            db.execSQL("DELETE FROM Course WHERE id=?", new Object[] { id });  
            db.close();  
        }  
      
       
        
        public Course nextCourse(Integer id ) {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT name,id FROM Course WHERE id>?  order by id asc limit 1", new String[] { id.toString() });  
            Course c = null;  
            if (cursor.moveToNext()) {  
                String name = cursor.getString(0);  
                c = new Course();  
            }  
            cursor.close();  
            db.close();  
            return c;  
        }  
        /**  
         * 查询所有Course对象  
         * @return Course对象集合, 如未找到, 返回一个size()为0的List  
         */  
        public List<Course> findAll() {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT id,name FROM Course", null);  
            List<Course> Courses = new ArrayList<Course>();  
            while (cursor.moveToNext()) {  
                Integer id = cursor.getInt(0);  
                String name = cursor.getString(1);  
                  
               
                Courses.add(new Course());  
            }  
            cursor.close();  
            db.close();  
            return Courses;  
        }  

        /**  
         * 查询某一页数据  
         * @param page 页码  
         * @param size 每页记录数  
         * @return  
         */  
        public List<Course> findPage(int page, int size) {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT id,name FROM Course  LIMIT ?,?" //  
                    , new String[] { String.valueOf((page - 1) * size), String.valueOf(size) });  
            List<Course> Courses = new ArrayList<Course>();  
            while (cursor.moveToNext()) {  
                Integer id = cursor.getInt(0);  
                String name = cursor.getString(1);  
                  
               
                Courses.add(new Course());  
            }  
            cursor.close();  
            db.close();  
            return Courses;  
        }  
        /**  
         * 查询某一页数据  
         * @param page 页码  
         * @param size 每页记录数  
         * @return  
         */  
        public int findPageCount(int page, int size) {  
        	SQLiteDatabase db = helper.getReadableDatabase();  
        	int count =0;
        	Cursor cursor = db.rawQuery("SELECT ID FROM Course where  id>? and id<?" //  
        			, new String[] { String.valueOf((page - 1)*size), String.valueOf(page*size) });  
        	count = cursor.getCount();  
        	cursor.close();  
        	db.close();  
        	return count;  
        }  

}  

